<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="供应商" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="燃料品名" prop="fuelName">
        <el-input
          v-model="queryParams.fuelName"
          placeholder="请输入燃料品名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="送料车辆" prop="carNum">
        <el-input
          v-model="queryParams.carNum"
          placeholder="请输入送料车辆"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约送达时间" prop="reservationDate" label-width="120">
        <el-date-picker clearable
          v-model="queryParams.reservationDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预约送达时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请输入状态" clearable>
          <el-option
            v-for="dict in poundStatusList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pound:pound:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="poundList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商" align="center" prop="supplierName" />
      <el-table-column label="燃料品名" align="center" prop="fuelName" />
      <el-table-column label="车辆" align="center" prop="carNum" />
      <el-table-column label="毛重(吨)" align="center" prop="grossWeight" />
      <el-table-column label="皮重(吨)" align="center" prop="tareWeight" />
      <el-table-column label="净重(吨)" align="center" prop="netWeight" />
      <el-table-column label="质检扣杂(吨)" align="center" prop="deductWeight" width="120"/>
      <el-table-column label="合同单价" align="center" prop="fuelContractPrice" />
      <el-table-column label="结算单价" align="center" prop="settlementUnitPrice" />
      <el-table-column label="结算金额" align="center" prop="settlementTotalPrice" />
      <el-table-column label="结算重量" align="center" prop="settlementWeight" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pound_status" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="预约送达时间" align="center" prop="reservationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reservationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row)"
            v-hasPermi="['pound:pound:edit']"
          >去结算</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="查看" :visible.sync="viewOpen" width="60%" append-to-body>
      <el-form ref="form" :model="form" label-width="120px">
        <el-tabs v-model="activeName">
            <el-tab-pane label="磅单信息" name="first">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="供应商名称" prop="supplierName">
                    {{form.supplierName}}
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="燃料品名" prop="fuelName">
                    {{form.fuelName}}
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="12">
                  <el-form-item label="车辆车牌" prop="carNum">
                    {{form.carNum}}
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="预约送达时间" prop="reservationTime">
                    <el-date-picker
                      :disabled="true"
                      v-model="form.reservationTime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="">
                    </el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="12">
                  <el-form-item label="车辆签到时间" prop="signTime">
                    <el-date-picker
                      :disabled="true"
                      v-model="form.signTime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="请选择车辆签到时间">
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="12">

                </el-col>
              </el-row>

              <el-row>
                <el-col :span="12">
                  <el-form-item label="毛重" prop="grossWeight">
                    {{form.grossWeight}}
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="皮重" prop="tareWeight">
                    {{form.tareWeight}}
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="12">
                  <el-form-item label="净重" prop="grossWeight">
                    {{form.netWeight}}
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="扣杂" prop="deductWeight">
                    {{form.deductWeight}}
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="24">
                  <el-form-item label="化验结果" prop="assayResult">
                    <div v-for="(item,index) in form.assayResult" :key="index">
                      <span>{{item.attrName}}：</span>
                      <span>{{item.value}}</span>
                    </div>
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="12">
                  <el-form-item label="结算金额" prop="settlementTotalPrice">
                    {{form.settlementTotalPrice}}
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="结算重量" prop="settlementWeight">
                    {{form.settlementWeight}}
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="12">
                  <el-form-item label="结算单价" prop="settlementUnitPrice">
                    {{form.settlementUnitPrice}}
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="状态" prop="state">
                    <dict-tag :options="dict.type.pound_status" :value="form.state"/>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-tab-pane>
            <el-tab-pane label="过磅照片" name="second">
              <el-row>
                <el-col :span="24">
                  <el-form-item label="过磅照片" prop="poundPic">
                    <div  style="margin: 10px;display: flex;">
                      <img :src="item"  v-for="(item,index) in form.poundPic" :key="index" style="width: calc(50%);height: 400px;margin: 10px;"></img>
                    </div>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-tab-pane>
            <el-tab-pane label="质检照片" name="third">
              <el-row>
                <el-col :span="24">
                  <el-form-item label="质检照片" prop="qualityPic">
                    <div  style="margin: 10px;display: flex;">
                      <img :src="item"  v-for="(item,index) in form.qualityPic" :key="index" style="width: calc(50%);height: 400px;margin: 10px;"></img>
                    </div>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-tab-pane>
            <!-- <el-tab-pane label="结算信息" name="fourth"></el-tab-pane> -->
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">标记为已结算</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSettlePound, getPound, delPound, addPound, updatePound } from "@/api/pound/pound";
import { listAttribute } from "@/api/factory/attribute";

export default {
  name: "Pound",
  dicts: ['pound_status'],
  data() {
    return {
      poundStatusList:[
        {
          label:'已化验',
          value: 5
        },
        {
          label:'待结算',
          value: 6
        },
        {
          label:'已结算',
          value: 7
        }
      ],
      viewOpen:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 磅单信息表格数据
      poundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierName: null,
        fuelName: null,
        carNum: null,
        state: null,
        orderByColumn:'id',
        isAsc:'descending'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        factoryId: [
          { required: true, message: "工厂id不能为空", trigger: "blur" }
        ],
        supplierId: [
          { required: true, message: "供应商id不能为空", trigger: "blur" }
        ],
        fuelId: [
          { required: true, message: "燃料品名id不能为空", trigger: "blur" }
        ],
        carId: [
          { required: true, message: "送料车辆id不能为空", trigger: "blur" }
        ],
        factoryName: [
          { required: true, message: "工厂名称不能为空", trigger: "blur" }
        ],
        supplierName: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" }
        ],
        fuelName: [
          { required: true, message: "燃料品名不能为空", trigger: "blur" }
        ],
        carNum: [
          { required: true, message: "车辆车牌不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "状态：0.待过磅，1.已过磅，2.已入库,3.已回皮,4.已质，5.已化验，6.已结算不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ]
      },
      attributeParams:{
        pageNum: 1,
        pageSize: 100,
        fuelId:0,
      },
      attributeList:[],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询磅单信息列表 */
    getList() {
      this.loading = true;
      listSettlePound(this.queryParams).then(response => {
        this.poundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getAttributeList(){
      listAttribute(this.attributeParams).then(response =>{
        this.attributeList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryId: null,
        supplierId: null,
        fuelId: null,
        carId: null,
        factoryName: null,
        supplierName: null,
        fuelName: null,
        carNum: null,
        reservationTime: null,
        signTime: null,
        weighTime: null,
        storeTime: null,
        poundPic: null,
        grossWeight: null,
        tareWeight: null,
        netWeight: null,
        qualityPic: null,
        deductWeight: null,
        assayResult: null,
        calcRules: null,
        settlementTime: null,
        settlementUnitPrice: null,
        settlementTotalPrice: null,
        settlementWeight: null,
        state: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加磅单信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPound(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改磅单信息";
      });
    },
    handleView(row){
      this.reset();
      const id = row.id || this.ids
      getPound(id).then(response => {
        this.form = response.data;
        if(response.data.assayResult){
          this.form.assayResult = JSON.parse(response.data.assayResult);
        }
        if(response.data.poundPic){
          this.form.poundPic = JSON.parse(response.data.poundPic);
        }
        if(response.data.qualityPic){
          this.form.qualityPic = JSON.parse(response.data.qualityPic);
        }
        this.activeName = "first";
        this.viewOpen = true;
      });
    },
    //上传磅单
    async handleForm(row){
      this.reset();
      const id = row.id || this.ids
      const result = await getPound(id);
      const data = result.data;
      if(data.state != 0){
        this.$modal.msgError("当前选择的不需要上传过磅信息");
        return;
      }
      this.attributeParams.fuelId = data.fuelId;
      this.getAttributeList();
      this.form = data;
      this.open = true;
      this.title = "磅单信息";
    },
    //质检
    async handleCheckForm(row){
      this.reset();
      const id = row.id || this.ids
      const result = await getPound(id);
      const data = result.data;
      if(data.state != 3){
        this.$modal.msgError("当前选择的无需质检");
        return;
      }
      this.attributeParams.fuelId = data.fuelId;
      this.getAttributeList();
      this.form = data;
      this.open = true;
      this.title = "磅单信息";
    },
    async handleAssayForm(row){
      this.reset();
      const id = row.id || this.ids
      const result = await getPound(id);
      const data = result.data;
      if(data.state != 4){
        this.$modal.msgError("当前选择的无需化验");
        return;
      }
      this.attributeParams.fuelId = data.fuelId;
      this.getAttributeList();
      this.form = data;
      this.open = true;
      this.title = "磅单信息";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            let dataForm = {
              id:this.form.id,
              state:7
            };
            updatePound(dataForm).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.viewOpen = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除磅单信息编号为"' + ids + '"的数据项？').then(function() {
        return delPound(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pound/pound/export', {
        ...this.queryParams
      }, `pound_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
